package com.cloud.easy.im.handler.strategy.websocket;

import com.cloud.easy.im.argeement.Agreement;
import com.cloud.easy.im.argeement.AgreementType;
import com.cloud.easy.im.event.ServerEventListener;
import com.cloud.easy.im.processor.CommonProcessor;
import com.cloud.easy.im.processor.OnlineProcessor;
import com.cloud.easy.im.utils.LocalSendHelper;
import com.cloud.easy.im.handler.strategy.AgreementStrategy;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 客户端发送通用推送数据(推送场景，只是使用与1v1场景)
 *
 * @author wangjian
 * @version 0.1.0
 * @create 2020年09月16日10:46 上午
 */
public class AgreementWebSocketCCommonPushHandler extends AgreementStrategy {

    private static final Logger logger = LoggerFactory.getLogger(AgreementWebSocketCLoginHandler.class);

    private final CommonProcessor commonProcessor;

    public AgreementWebSocketCCommonPushHandler() {
        super(AgreementType.C.FROM_CLIENT_TYPE_OF_COMMON$PUSH);
        commonProcessor = CommonProcessor.getInstance();
    }

    @Override
    public void handler(Channel session, Agreement dataFromClient, ServerEventListener serverEventListener) throws Exception {
        logger.debug("[WS_COMMON$PUSH]<< 收到客户端的通用数据发送请求. Agreement: " + dataFromClient.toJsonString());

        if (serverEventListener != null) {
            //如果发送者不在线
            if (!OnlineProcessor.isOnline(session)) {
                LocalSendHelper.replyDataForUnlogined(session, dataFromClient, null);
                return;
            }

            if (Agreement.serverMark.equals(dataFromClient.getTo())) {
                //发送给服务端
                //TODO 后续待实现
            } else {
                //发送给客户端
                commonProcessor.processC2CMessage(session, dataFromClient, serverEventListener);
            }
        } else {
            logger.error("[WS_COMMON$PUSH]<< 收到客户端的通用数据传输消息，但业务端监听器是null，回调无法继续.");
        }
    }
}
